package RevolCraft.Handlers;

import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;

import RevolCraft.TileEntities.TileEntityElectricFurnace;
import RevolCraft.TileEntities.TileEntityGenerator;

import net.minecraft.client.Minecraft;
import net.minecraft.src.INetworkManager;
import net.minecraft.src.Packet250CustomPayload;
import net.minecraft.src.TileEntity;
import net.minecraft.src.World;
import net.minecraft.src.WorldServer;
import net.minecraftforge.common.DimensionManager;
import cpw.mods.fml.common.network.IPacketHandler;
import cpw.mods.fml.common.network.Player;

public class PacketHandler implements IPacketHandler {

	@Override
	public void onPacketData(INetworkManager manager, Packet250CustomPayload packet, Player player) {
		if (packet.channel.equals("TileEntities")) {
			switch(packet.data[0])
			{
			case 0: handleGenerator(packet);
					break;
			case 1: handleElectricFurnace(packet);
					break;
			}
		}
	}

	public void handleGenerator(Packet250CustomPayload packet)
	{
		DataInputStream inputStream = new DataInputStream(new ByteArrayInputStream(packet.data));
		
		int x, y, z;
		int burnTime;
		int itemBurnTime;
		int currentStorage;
		int maxStorage;
		
		try
		{
			inputStream.readByte();
			x = inputStream.readInt();
			y = inputStream.readInt();
			z = inputStream.readInt();
			burnTime = inputStream.readInt();
			itemBurnTime = inputStream.readInt();
			currentStorage = inputStream.readInt();
			maxStorage = inputStream.readInt();
		}
		catch (IOException e) {
			e.printStackTrace();
			return;
		}
		TileEntity tileEntity = Minecraft.getMinecraft().theWorld.getBlockTileEntity(x, y, z);
		TileEntityGenerator te = (TileEntityGenerator)tileEntity;
		te.burnTime = burnTime;
		te.currentItemBurnTime = itemBurnTime;
		te.currentStorage = currentStorage;
		te.maxStorage = maxStorage;
	}
	
	public void handleElectricFurnace(Packet250CustomPayload packet)
	{
		DataInputStream inputStream = new DataInputStream(new ByteArrayInputStream(packet.data));
		
		int x, y, z;
		int cookTime;
		int currentStorage;
		int maxStorage;
		
		try
		{
			inputStream.readByte();
			x = inputStream.readInt();
			y = inputStream.readInt();
			z = inputStream.readInt();
			cookTime = inputStream.readInt();
			currentStorage = inputStream.readInt();
			maxStorage = inputStream.readInt();
		}
		catch (IOException e) {
			e.printStackTrace();
			return;
		}
		TileEntity tileEntity = Minecraft.getMinecraft().theWorld.getBlockTileEntity(x, y, z);
		TileEntityElectricFurnace te = (TileEntityElectricFurnace)tileEntity;
		te.cookTime = cookTime;
		te.currentStorage = currentStorage;
		te.maxStorage = maxStorage;
	}
}
